Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@universal-packages/validations
Advanced tools
Simple validation system based on decorators to enable a class to validate a subject properties.
Simple validation system based on decorators to enable a class to validate a subject properties.
npm install @universal-packages/validations
Extend the base validation to start building a class validation, when running the validation you will get validation record containing errors if any and a valid
flag to quick knowing if if was successful.
import { BaseValidation, Validator } from '@universal-packages/validations'
export default class UserValidation extends BaseValidation {
@Validator('name')
rightNameSize(value) {
return value.length > 5 && value.length < 128
}
}
console.log(await UserValidation.validate({ name: 'sm' }))
// > { errors: { name: ['name failed rightNameSize validation'] }, valid: false }
You can pass initial values so that you can reference them in your validators, for example when you don't want to perform heavy validations on values that were already validated.
import { BaseValidation, Validator } from '@universal-packages/validations'
export default class UpdateUserValidation extends BaseValidation {
@Validator('email')
alreadyInDb(value, initialName) {
if (value === initialName) return true
return !await db.exists({ email: value })
}
}
const validation = new UpdateUserValidation({ name: 'email' })
console.log(await validation.validate({ name: 'email' }))
// > { errors: {}, valid: true }
@Validator(property: string, [options])
The validator decorator enable a class method to act as a validator, the method should return a boolean to tell teh validation if the property is valid or not. The first argument of the decorator is the property to be validated.
You can use several methods to validate a single property:
import { BaseValidation, Validator } from '@universal-packages/validations'
export default class UserValidation extends BaseValidation {
@Validator('name')
isAString(value) {
return typeof value === 'string'
}
@Validator('name')
rightNameSize(value) {
return value.length > 5 && value.length < 128
}
}
console.log(await UserValidation.validate({ name: 50 }))
// > { errors: { name: ['name failed isAString validation', 'name failed rightNameSize validation'] }, valid: false }
inverse
Boolean
Inverts the validator validity of the method returns true the property is invalid.
@Validator('name', { inverse: true })
isPretty(value) {
return value === 'ugly'
}
message
String
When the validation fails set the error with a custom message.
@Validator('name', { message: 'Name is not pretty' })
isPretty(value) {
return value !== 'ugly'
}
optional
Boolean
The validation will run only if the property is set (not undefined nor null).
@Validator('name', { optional: true })
isStrong(password) {
return password.length > 69
}
priority
Number
The priority level for the validation, if a validation with a lower number fails validations with a upper number will not run, but all validations in the same priority will run.
Use this so validations don't throw an error reading an unexpected type.
@Validator('name')
isString(value) {
return typeof value === 'string'
}
@Validator('name', { priority: 1})
containsWord(value) {
return value.indexOf('word') !== -1
}
This library is developed in TypeScript and shipped fully typed.
The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.
FAQs
Simple validation system based on decorators to enable a class to validate a subject properties.
The npm package @universal-packages/validations receives a total of 1,313 weekly downloads. As such, @universal-packages/validations popularity was classified as popular.
We found that @universal-packages/validations demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.